package com.nageoffer.shortlink.project.mq.producer;

import org.springframework.kafka.core.KafkaTemplate;
import org.springframework.stereotype.Component;

@Component
public class KafkaProducerService {
    private final KafkaTemplate<String, Object> kafkaTemplate;

    public KafkaProducerService(KafkaTemplate<String, Object> kafkaTemplate) {
        this.kafkaTemplate = kafkaTemplate;
    }

    /**
     * 发送简单消息
     */
    public void sendMessage(String topic, String message) {
        kafkaTemplate.send(topic, message);
    }

    /**
     * 发送带Key的消息
     */
    public void sendMessageWithKey(String topic, String key, String message) {
        kafkaTemplate.send(topic, key, message);
    }

    /**
     * 发送对象消息（自动序列化为JSON）
     */
    public void sendObjectMessage(String topic, Object data) {
        kafkaTemplate.send(topic, data);
    }

    /**
     * 发送到指定分区
     */
    public void sendToPartition(String topic, int partition, String key, Object data) {
        kafkaTemplate.send(topic, partition, key, data);
    }
}
